/*
 * Copyright (c) 2009, 2010, 2011, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.latke.servlet;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Front controller for HTTP request dispatching.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.1, Jul 16, 2011
 */
public final class HTTPRequestDispatcher extends AbstractHTTPRequestDispatcher {

    /**
     * Default serial version uid.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Logger.
     */
    private static final Logger LOGGER =
            Logger.getLogger(HTTPRequestDispatcher.class.getName());
    /**
     * Request processing objects.
     */
    private static final Map<String, Object> PROCESSING_OBJS =
            new HashMap<String, Object>();

    static {
        <#list requestProcessingClasses as processingClass>
    PROCESSING_OBJS.put("${processingClass.classCanonicalName}", new ${processingClass.classCanonicalName}());
        <#if processingClass_has_next>
                    
        </#if>       
        </#list>
    }

    @Override
    protected void dispatch(final HTTPRequestContext context) {
        final HttpServletRequest request = context.getRequest();
        final HttpServletResponse response = context.getResponse();

        final String method = request.getMethod();
        final String requestURI = request.getRequestURI();

        final RequestKey requestKey = new RequestKey();
        requestKey.setMethod(method);
        requestKey.setRequestURI(requestURI);
        
        LOGGER.log(Level.FINER, "Request Key[{0}]", requestKey.toString());

        <#list requestProcessingClasses as processingClass>
        <#list processingClass.processingMethods as method>
        if (match(requestKey, "${method.method}", "${method.requestURI}")) {
            ((${processingClass.classCanonicalName})PROCESSING_OBJS.get("${processingClass.classCanonicalName}"))
                .${method.processingMethodName}(context);
        }
        <#if processingClass_has_next>
        
        </#if>
        </#list>
        </#list>
        
        final AbstractHTTPResponseRenderer renderer = context.getRenderer();
        if (null == renderer) {
        LOGGER.log(Level.WARNING, "Renderer is null");
            // TODO: default renderer?
            return;
        }
        
        renderer.render(context);
    }
}
